package cn.itcast.nems.oldems.service.impl;

import cn.itcast.nems.discount.api.DiscountApi;
import cn.itcast.nems.discount.dto.DiscountSummaryDTO;
import cn.itcast.nems.discounts.dao.entiry.EmsDiscounts;
import cn.itcast.nems.discounts.dao.mapper.EmsDiscountsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DiscountServiceImpl extends ServiceImpl<EmsDiscountsMapper, EmsDiscounts> implements DiscountApi {

    @Override
    public DiscountSummaryDTO getSummaryById(String id) {
        EmsDiscounts discountEntity = getBaseMapper().selectById(id);
        return toDiscountSummaryDTO(discountEntity);
    }

    @Override
    public Set<DiscountSummaryDTO> getSummaryByIds(Set<String> discountIds) {
        final List<EmsDiscounts> emsDiscounts = super.getBaseMapper().selectBatchIds(discountIds);
        if (emsDiscounts == null || emsDiscounts.isEmpty()) {
            return Collections.emptySet();
        }
        return emsDiscounts
                .stream()
                .map(this::toDiscountSummaryDTO)
                .collect(Collectors.toSet());
    }

    private DiscountSummaryDTO toDiscountSummaryDTO(EmsDiscounts discountEntity) {
        DiscountSummaryDTO result = new DiscountSummaryDTO();
        result.setId(discountEntity.getId());
        result.setName(discountEntity.getDiscountsName());
        result.setType(discountEntity.getDiscountsStatus());
        result.setAmount(discountEntity.getDiscountsMoney());
        result.setRate(discountEntity.getDiscountsRate());
        result.setDescription(discountEntity.getDescription());
        result.setState(discountEntity.getStatus());
        result.setCreatedBy(discountEntity.getEpRealName());
        result.setCreatedDateTime(discountEntity.getCreateTime());
        return result;
    }
}
